import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.*;

/* *********************************************************** *\
    ICSFormat Class
    -A static class. Provides data checks and reformats stuff. 

    Change Log
    -7/30 wrote valid_syntax(); unsure about what to do with errors. currently just prints a stored msg. 

    Functions
    -valid_syntax(type,key,value)   |   checks syntax only to see if valid. returns error msg.
    -valid_value(type,key,value)    |   checks value. does type conversions and returns proper format. 
    
\* *********************************************************** */
public class ICSFormat{
    private ICSFormat(){
    }
    //Intended for realtime syntax only checks. 
    //formatting and context checks can happen on submit instead.
    
    public static void main(String[] args){
        String result=valid("time","11:57");
        result=valid("time","11:57:23");        
        System.out.println(result);
        
        result=valid("time","24:00:23");        
        System.out.println(result);
        
        result=valid("time","24:01:23");        
        System.out.println(result);
        
        result=timestamp();     
        System.out.println(result);
        
    }
    public static int compareDate(String first, String second){
        first=valid("date",first);
        second=valid("date",second);
        if(first.length()>1&&second.length()>1){
            try{
                if(Integer.parseInt(second)>Integer.parseInt(first))
                    return 1;            
                if(Integer.parseInt(second)==Integer.parseInt(first))
                    return 0;                          
                }
            catch(Exception e){
            }
        }
        return -1;
    }
    public static int compareTime(String first, String second){
        first=valid("time",first);
        second=valid("time",second);
        if(first.length()>3&&second.length()>3){
            try{
                if(Integer.parseInt(second.substring(1))>Integer.parseInt(first.substring(1)))
                    return 1;            
                if(Integer.parseInt(second.substring(1))==Integer.parseInt(first.substring(1)))
                    return 0;        
                }
            catch(Exception e){        
            }
        }
        return -1;
    }
    public static String timestamp(){
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return valid("date",dateFormat.format(date))+valid("time",timeFormat.format(date));
    }
    public static String valid(String type, String value){
        //"" is success. otherwise, returns proper errorMsg
        String[] errorMsg;
        String error="Failed to check cases.";
        switch(Arrays.asList("date","time","text").indexOf(type)){
            case 0: //date type
                if(valid_date(value)){
                    String[] array = value.split("/");
                    if(Integer.parseInt(array[1])<10){
                        array[1]="0"+array[1];
                    }
                    if(Integer.parseInt(array[2])<10){
                        array[2]="0"+array[1];                        
                    }
                    return array[2]+array[0]+array[1];
                }
                return "";
            case 1: //time type
                String output=valid_time(value);
                return (output.length()<6&&output.length()>2)?output+"00":output;
                
            case 2: //text type
                return valid_text( value);
                
            default://bad input
                error="Failed to match type";
                break;
        }
        return error;

    }
        private static boolean valid_date(String value){
            try {
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                df.setLenient(false);
                df.parse(value).toString();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        private static String valid_time(String value){
            //valid syntaxes: 12:01am, 24:00, 15:01:40, 1:29 pm
            if(value.matches("(([0-9]|[0-1][0-9]|2[0-3]):[0-5][0-9]|24:00)(|:[0-5][0-9])"))                
                return "T"+value.replace(":",""); 
            return "";
        }
        private static String valid_text(String value){
            //whitelist valid characters a-z, 0-9, ._/?!, space 
            if(value.matches("^[0-9a-zA-Z\\.,?!_/\\s]{1,}$")) 
                return value;
            return "";
        }

}
